package cn.zbmao.web.services;

import cn.zbmao.web.bean.Qualification;
import cn.zbmao.web.util.dao.DaoFactory;
import com.jfinal.plugin.activerecord.Record;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 关注招标信息资质服务 Created by 愤怒的番茄 on 2014/10/8.
 */
public class QualificationServices {
//    缓存资质信息到map中，可以通过Id查询到资质
	private static Map<Integer, Qualification> qualificationKeyMap = new HashMap<Integer, Qualification>();
//缓存资质列表
	private static List<Record> qualificationList = new ArrayList<Record>();
// 对资质经行初始化
	static {
		init();
	}

	private static void init() {
		// 获取key列表
		qualificationList = DaoFactory.getQualificationDao()
				.findQualificationList();
		// 获取全部
//将资质存入静态的map中
		for (Record record : qualificationList) {
			setQualificationMap(record);
		}

	}
// 资质信息存到Map中
	private static void setQualificationMap(Record record) {
		int id = record.getInt("id");

		Qualification qualification = new Qualification();
		qualification.setId(record.getInt("id"));
		qualification.setName(record.getStr("name"));
		qualification.setSeachTerm(new String[] { record.getStr("key") });
		
		
		List<Record> list = DaoFactory.getQualificationDao().findQualificationOther(qualification.getName());
		for (Record record2 : list) {
			qualification.setSeachTerm((String[]) ArrayUtils.add(qualification.getSeachTerm(), record2.getStr("key")));
		}
		qualificationKeyMap.put(id, qualification);
		
	}

	/**
	 * 清除缓存
	 */
	public static void clear() {
		qualificationKeyMap.clear();
		qualificationList.clear();
		init();
	}

    /**
     * 获取资质列表
     * @return
     */
	public static List<Record> getQualificationList() {
		return qualificationList;
	}

    /**
     * 获取资质Map
     * @return
     */
	public static Map<Integer, Qualification> getQualificationMap() {
		return qualificationKeyMap;
	}

	public static void insert(Qualification qualification) {

	}

    /**
     * 显示资质，如果资质中包含用户设置的资质，给其添加高亮
     * @param qualification
     * @param list
     * @return
     */
    public static String getQualificationForShow(String qualification,List<String> list) {
        if (StringUtils.isBlank(qualification)){
            return "";
        }
        StringBuffer qualificationStringBuffer = new StringBuffer();




        String[] qualificationStrs = qualification.split(" ");
//       给包含用户设置的资质添加高亮
        for (String qualificationStr : qualificationStrs) {
            if(list.size()>0&&list.contains(qualificationStr)){
                qualificationStringBuffer.append("<span class=\'highlight hot\'>"+qualificationStr+"</span>,");
            }else{
                qualificationStringBuffer.append(qualificationStr+",");
            }


        }
         return qualificationStringBuffer.toString().substring(0,qualificationStringBuffer.length()-1);
    }

    public static String getQualificationForShow(String qualification){
        return getQualificationForShow(qualification,new ArrayList<String>());
    }

}
